//
//  LGRecoverySportsProgramCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/7/31.
//  运动方案--训练目录

#import "LGRecoverySportsProgramCell.h"

#define itemWidth  viewPix(59)

@interface LGRecoverySportsProgramCell()
/**   */
@property (nonatomic , strong)UIImageView *picView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UIImageView *tipView1;
/**   */
@property (nonatomic , strong)UILabel *contentLabel;
/**   */
@property (nonatomic , strong)UIImageView *tipView2;
/**   */
@property (nonatomic , strong)UIButton *upButton;
/**   */
@property (nonatomic , strong)UIButton *downButton;
/**   */
@property (nonatomic , strong)UIButton *deleteBtn;
/**   */
@property (nonatomic , strong)UIButton *titleButton;
/**   */
@property (nonatomic , strong)UIButton *contentBtn;

@end

@implementation LGRecoverySportsProgramCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return  self;
}

-(void)setPosition:(NSString *)position{
    _position = position;
    if([position isEqualToString:@"first"]){
        //第一个 -- 可以下移，不能上移
        self.upButton.selected = NO;
        self.downButton.selected = YES;
        self.upButton.userInteractionEnabled = NO;
        self.downButton.userInteractionEnabled = YES;
        self.upButton.layer.borderColor = [UIColor colorWithString:@"#E0E0E0"].CGColor;
        self.downButton.layer.borderColor = [UIColor colorWithString:@"#3291FF"].CGColor;
        
    }else if ([position isEqualToString:@"last"]){
        //最后一个 -- 不能下移，可以上移
        self.upButton.selected = YES;
        self.downButton.selected = NO;
        self.upButton.userInteractionEnabled = YES;
        self.downButton.userInteractionEnabled = NO;
        self.upButton.layer.borderColor = [UIColor colorWithString:@"#3291FF"].CGColor;
        self.downButton.layer.borderColor = [UIColor colorWithString:@"#E0E0E0"].CGColor;
        
    }else if ([position isEqualToString:@"middle"]){
        //中间的 -- 可以下移，可以上移
        self.upButton.selected = YES;
        self.downButton.selected = YES;
        self.upButton.userInteractionEnabled = YES;
        self.downButton.userInteractionEnabled = YES;
        self.upButton.layer.borderColor = [UIColor colorWithString:@"#3291FF"].CGColor;
        self.downButton.layer.borderColor = [UIColor colorWithString:@"#3291FF"].CGColor;
    }else{
        //一个cell -- 不能下移，不能上移
        self.upButton.selected = NO;
        self.downButton.selected = NO;
        self.upButton.userInteractionEnabled = NO;
        self.downButton.userInteractionEnabled = NO;
        self.upButton.layer.borderColor = [UIColor colorWithString:@"#E0E0E0"].CGColor;
        self.downButton.layer.borderColor = [UIColor colorWithString:@"#E0E0E0"].CGColor;
    }
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    [self.picView sd_setImageWithURL:[NSURL URLWithString:LGNSString(dataDic[@"cover_url"])]];
    self.titleLabel.text = [NSString stringWithFormat:@"%.2ld  %@·%@",self.indexPath.row+1,dataDic[@"category_name"],dataDic[@"title"]];
    if([LGNSString(dataDic[@"duration"]) isEqualToString:@"无"]){
        self.contentLabel.text = [NSString stringWithFormat:@"共 %@ 组， 每组 %@ 次",dataDic[@"group_times"],dataDic[@"action_times"]];
    }else{
        self.contentLabel.text = [NSString stringWithFormat:@"共 %@ 组， 每组 %@ 次， 每次 %@ 秒",dataDic[@"group_times"],dataDic[@"action_times"],dataDic[@"duration"]];
    }
}

-(void)setIndex:(NSInteger)index{
    _index = index;
    self.titleLabel.text = [NSString stringWithFormat:@"%.2ld  %@·%@",index+1,self.dataDic[@"category_name"],self.dataDic[@"title"]];
}

//更换项目
-(void)titleButtonAction{
    if(self.delegate && [self.delegate respondsToSelector:@selector(editSportsProgram:indexPath:)]){
        [self.delegate editSportsProgram:@"title" indexPath:self.indexPath];
    }
}


//设置组数
-(void)contentBtnAction{
    if(self.delegate && [self.delegate respondsToSelector:@selector(editSportsProgram:indexPath:)]){
        [self.delegate editSportsProgram:@"content" indexPath:self.indexPath];
    }
}

//上移
-(void)upButtonAction{
    if(self.delegate && [self.delegate respondsToSelector:@selector(moveProgramPosition:indexPath:)]){
        [self.delegate moveProgramPosition:@"up" indexPath:self.indexPath];
    }
}
//下移
-(void)downButtonAction{
    if(self.delegate && [self.delegate respondsToSelector:@selector(moveProgramPosition:indexPath:)]){
        [self.delegate moveProgramPosition:@"down" indexPath:self.indexPath];
    }
}
//删除
-(void)deleteButtonAction{
    if(self.delegate && [self.delegate respondsToSelector:@selector(removeProgramAction:)]){
        [self.delegate removeProgramAction:self.indexPath];
    }
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    [self.contentView addSubview:self.picView];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.tipView1];
    [self.contentView addSubview:self.contentLabel];
    [self.contentView addSubview:self.tipView2];
    [self.contentView addSubview:self.upButton];
    [self.contentView addSubview:self.downButton];
    [self.contentView addSubview:self.deleteBtn];
    [self.contentView addSubview:self.lineView];
    [self.contentView addSubview:self.titleButton];
    [self.contentView addSubview:self.contentBtn];
    [self.picView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(viewPix(12));
        make.left.equalTo(self.contentView).offset(viewPix(16));
        make.width.equalTo(@(viewPix(99)));
        make.height.equalTo(@(viewPix(62)));
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.picView);
        make.left.equalTo(self.picView.mas_right).offset(viewPix(8));
        make.right.equalTo(self.tipView1.mas_left);
    }];
    [self.tipView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-viewPix(10));
        make.centerY.equalTo(self.titleLabel);
        make.width.height.equalTo(@(viewPix(12)));
    }];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(viewPix(5));
        make.left.right.equalTo(self.titleLabel);
    }];
    [self.tipView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.width.height.equalTo(self.tipView1);
        make.centerY.equalTo(self.contentLabel);
    }];
    [self.upButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLabel.mas_bottom).offset(viewPix(17));
        make.right.equalTo(self.downButton.mas_left).offset(-viewPix(8));
        make.width.equalTo(@(viewPix(60)));
        make.height.equalTo(@(viewPix(24)));
    }];
    [self.downButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.deleteBtn.mas_left).offset(-viewPix(8));
        make.centerY.equalTo(self.upButton);
        make.width.equalTo(@(viewPix(60)));
        make.height.equalTo(@(viewPix(24)));
    }];
    [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-viewPix(16));
        make.centerY.equalTo(self.upButton);
        make.width.equalTo(@(viewPix(60)));
        make.height.equalTo(@(viewPix(24)));
    }];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.upButton.mas_bottom).offset(viewPix(12));
        make.left.equalTo(self.contentView).offset(viewPix(16));
        make.right.equalTo(self.contentView).offset(-viewPix(16));
        make.bottom.equalTo(self.contentView);
        make.height.equalTo(@(0.5));
    }];
    
    [self.titleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.equalTo(self.titleLabel);
        make.right.equalTo(self.contentView);
    }];
    
    [self.contentBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.equalTo(self.contentLabel);
        make.right.equalTo(self.contentView);
    }];
}

-(UIImageView *)picView{
    if(!_picView){
        _picView = [[UIImageView alloc]init];
        _picView.layer.borderColor = [UIColor colorWithString:@"#999999"].CGColor;
        _picView.layer.borderWidth = 1;
        _picView.cornerRidus = 10;
    }
    return _picView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"01 热身运动·颈部环绕" colorString:@"#333333" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _titleLabel;
}

-(UIImageView *)tipView1{
    if(!_tipView1){
        _tipView1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"grayTip12"]];
    }
    return _tipView1;
}

-(UILabel *)contentLabel{
    if(!_contentLabel){
        _contentLabel = [UILabel lableWithText:@"共" colorString:@"#666666" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _contentLabel;
}

-(UIImageView *)tipView2{
    if(!_tipView2){
        _tipView2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"grayTip12"]];
    }
    return _tipView2;
}


-(UIButton *)upButton{
    if(!_upButton){
        _upButton = [UIButton buttonWithTitle:@"上移" titleFont:LGFont(14) normalColor:@"#E0E0E0" selectColor:@"#3291FF"];
        _upButton.layer.borderColor = [UIColor colorWithString:@"#E0E0E0"].CGColor;
        _upButton.layer.borderWidth = 1;
        _upButton.cornerRidus = viewPix(12);
        [_upButton addTarget:self action:@selector(upButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _upButton;
}

-(UIButton *)downButton{
    if(!_downButton){
        _downButton = [UIButton buttonWithTitle:@"下移" titleFont:LGFont(14) normalColor:@"#E0E0E0" selectColor:@"#3291FF"];
        _downButton.layer.borderColor = [UIColor colorWithString:@"#E0E0E0"].CGColor;
        _downButton.layer.borderWidth = 1;
        _downButton.cornerRidus = viewPix(12);
        [_downButton addTarget:self action:@selector(downButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _downButton;
}

-(UIButton *)deleteBtn{
    if(!_deleteBtn){
        _deleteBtn = [UIButton buttonWithTitle:@"删除" titleFont:LGFont(14) normalColor:@"#DD3030" selectColor:@"#DD3030"];
        _deleteBtn.layer.borderColor = [UIColor colorWithString:@"#DD3030"].CGColor;
        _deleteBtn.layer.borderWidth = 1;
        _deleteBtn.cornerRidus = viewPix(12);
        [_deleteBtn addTarget:self action:@selector(deleteButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteBtn;
}

-(UIView *)lineView{
    if(!_lineView){
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = [UIColor colorWithString:@"#E0E0E0"];
    }
    return _lineView;
}

-(UIButton *)titleButton{
    if(!_titleButton){
        _titleButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_titleButton addTarget:self action:@selector(titleButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _titleButton;
}

-(UIButton *)contentBtn{
    if(!_contentBtn){
        _contentBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [_contentBtn addTarget:self action:@selector(contentBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _contentBtn;
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
